<?php

class DefaultController extends BackOfficeController
{
	public function actionIndex()
	{
		$this->render('index');
	}
    
    /**
    * Login in BackOffice
    */
    public function actionLogin(){
        //Yii::import('User.models.AdminUser');
        $user = new User('login');
        $invalidStatusCase = false;
        $errorMsgs = array();
        if (Yii::app()->request->IsPostRequest) {
            Yii::app()->authManager->UserClass = 'User.models.User';
            Yii::app()->authManager->UserStatefulFields = array('username', 'email');
            
            $result = FSM::run('User.user.login', $_POST);
            $user = $result->model;
            if ($this->post('ajax','') == 'user-login-form'){
                echo $result->getActiveErrorMessages($user);
                Yii::app()->end();
            }
            if (! $result->hasErrors()){
                if (($url = Yii::app()->user->returnUrl) != '')
                    Yii::app()->controller->redirect($url);
                else
                    $this->redirect(Yii::app()->request->getBaseUrl(true));
            }
            else
            {
                $errors = $result->getErrors();
                if(isset($errors['ErrorCode']) && is_array($errors['ErrorCode']))
                    $errorMsgs = $errors['ErrorCode'];
            }
        }
        if (Yii::app()->user->isGuest === false) {
            $this->redirect(Yii::app()->request->getBaseUrl(true));
        }
        Yii::app()->layout = 'login';
        $this->render('login', array('user' => $user,'errorMsgs'=>$errorMsgs));
    }
    
    /**
    * Login in BackOffice
    */
    //public function actionLogin(){
//        if (Yii::app()->user->isGuest === false) {
//            $this->redirect(Yii::app()->request->getBaseUrl(true));
//        }
//        
//        $user = new FUser('login');
//        $this->performAjaxValidation($user);
//        
//        if (Yii::app()->request->IsPostRequest) {
//            $result = FSM::run('Core.Account.login', $_POST);
//            if ($result->hasErrors()){
//                $user->attributes = $_POST['FUser'];
//                $user->password = '';
//                $user->addError('password', 'Invalid password');
//            } else {
//                if (($url = Yii::app()->user->returnUrl) != '')
//                    Yii::app()->controller->redirect($url);
//                else
//                    $this->redirect(Yii::app()->request->getBaseUrl(true));            
//            }
//        }
//        
//        Yii::app()->layout = null;
//        $this->render('login', array('user' => $user));
//    }
    
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='login')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
    
    public function actionForgotPassword()
    {
        //Yii::import('User.models.AdminUser');
        $user = new User('forgot');
        $resetPasswordSuccess = false;
        if (Yii::app()->request->isPostRequest)
        {
            $result = FSM::run('User.User.forgotPassword', $_POST);
            $user = $result->model;
            if (!$result->hasErrors())
                $resetPasswordSuccess = true;
        }

        Yii::app()->layout = '';
        $this->render('forgotPassword', array('user' => $user,'resetPasswordSuccess'=>$resetPasswordSuccess));
    }
}